library(data.table)
library(dplyr)
library(reshape2)
library(sampling)
library(caret)
library(party)
library(pROC)

setwd('F:/ZHZ_R/LOST/')

#-----1 基于3+N、4+N、5+N 月数据建立分类器，参考查全82%，差准44%-----
fun1<-function(fname1,rs,ns,fname2,mark){
  dt<-fread(fname1,
            sep=',',integer64 = 'numeric')
  dt<-as.data.frame(dt)
  dt<-dt[,rs]
  names(dt)[1:7]<-ns
  dt<-as.data.table(dt)
  dt2<-fread(fname2,
             sep=',',integer64 = 'numeric')
  dt<-dt2[dt,on='mobile']
  dt[is.na(dt)]<-0
  for(i in 2:length(names(dt))){
    names(dt)[i]<-paste(names(dt)[i],mark,sep="")
  }
  return(dt)
}
#导入-2 -1月数据
dt.3.all<-fun1('201804_RFV_全量清单.csv',
               c('mobile','changke201804',"xia","zhong","shang","rfv_f","vv_sum"),
               c('mobile','changke',"xia","zhong","shang","days","vv_sum"),
               'RFV_V_按pv汇总_201804.csv',3)#

dt.4.all<-fun1('201805_RFV_全量清单.csv',
               c('mobile','ck',"xia","zhong","shang","rfv_f","vv_sum"),
               c('mobile','changke',"xia","zhong","shang","days","vv_sum"),
               'RFV_V_按pv汇总_201805.csv',4)#

dt.4.all<-dt.3.all[dt.4.all,on='mobile']
dt.4.all[is.na(dt.4.all)]<-0
rm(dt.3.all)

dt.4.all<-dt.4.all[,avg_sum3:=vv_sum3/days3]
dt.4.all<-dt.4.all[,avg_sum4:=vv_sum4/days4]
dt.4.all[is.na(dt.4.all)]<-0

test<-dt.4.all[,.(
  mobile,
  busi=业务4-业务3,
  qiandao=签到4-签到3,
  chaxun=查询4-查询3,
  daohang=底部切换4-底部切换3,
  digital=数字化内容4-数字化内容3,
  changke=changke3+changke4,
  shang=shang4-shang3,
  zhong=zhong4-zhong3,
  xia=xia4-xia3,
  days=days4-days3,
  vv_sum=vv_sum4-vv_sum3,
  avg_sum=avg_sum4-avg_sum3
)]

dt.4.all<-test[dt.4.all,on='mobile']
dt.4.all[is.na(dt.4.all)]<-0
rm(test)

#导入0月数据
dt.5<-fread('RFV_F_201806.csv',
            sep=',',integer64 = 'numeric')
dt.5<-dt.5[,.(mobile,lost_5=0)]

dt.4.all<-dt.5[dt.4.all,on='mobile']
dt.4.all[is.na(dt.4.all)]<-1
rm(dt.5)

#建模建立分类器
dt<-dt.4.all
rm(dt.4.all)
cl<-c(names(dt)[!names(dt) %in% 
                  c('mobile','lost_5',"分享3","业务3" ,"签到3" ,"礼包3" , "查询3",
                    "充值3" ,"登录设置等其他服务3","底部切换3","关闭弹框3",
                    "自动3","搜索3","活动3","数字化内容3","qita3","changke3",
                    "xia3","zhong3","shang3","days3","vv_sum3","avg_sum3",
                    "礼包4" ,"登录设置等其他服务4","关闭弹框4","qita4","vv_sum4",
                    "vv_sum","自动4","分享4")])
#抽样
train.id<-strata(dt,stratanames = "lost_5",size = c(30000,30000),description=T)#存留、流失
train.dt<-getdata(dt,train.id)
train.dt<-as.data.frame(train.dt)
train.dt$lost_5<-as.factor(train.dt$lost_5)
train.dt<-train.dt[,c("lost_5",cl)]

#抽取测试集
dt.0.t<-as.data.frame(dt)
test.dt<-dt.0.t[sample(nrow(dt.0.t),10000),c("lost_5",cl)]
test.dt.all<-dt.0.t[,c("lost_5",cl)]
test.dt.input<-test.dt[,cl]
rm(dt.0.t)

#选中参数，开始预测
cl.LR.SELECT<-c('chaxun','changke','签到4','xia4','days4','avg_sum4')
train.LR.TEMP<-train.dt[,c("lost_5",cl.LR.SELECT)]
test.LR.TEMP<-test.dt.all[,c("lost_5",cl.LR.SELECT)]
train.LR.TEMP$lost_5<-as.factor(train.LR.TEMP$lost_5)
model_LR<-glm(lost_5 ~ .,family=binomial(link='logit'),data=train.LR.TEMP)
summary(model_LR)
pre<-predict(model_LR,type='response')
r_c<-roc(train.dt$lost_5, pre, plot=TRUE, print.thres=TRUE, print.auc=TRUE)  

c.pred<-predict(model_LR,test.LR.TEMP,type='response')
c.pred <- ifelse( c.pred > 0.5,1,0)
a<-t(table(c.pred,test.LR.TEMP$lost_5))
LR_recall<-a[2,2]/(a[2,1]+a[2,2])
LR_precision<-a[2,2]/(a[1,2]+a[2,2])
LR_F1<-2*LR_recall*LR_precision/(LR_recall+LR_precision)
LR_F2<-5*LR_recall*LR_precision/(LR_recall+4*LR_precision)

print(a)
print(LR_recall)
print(LR_precision)
print(LR_F1)

rm(dt,r_c,test.dt,test.dt.all,test.dt.input,
   test.LR.TEMP,train.dt,train.id,train.LR.TEMP)

#-----2分类器建立完成，处理4+N-5+N月数据进行预测-----

#导入-2 -1月数据
dt.3.all<-fun1('201805_RFV_全量清单.csv',
               c('mobile','ck',"xia","zhong","shang","rfv_f","vv_sum"),
               c('mobile','changke',"xia","zhong","shang","days","vv_sum"),
               'RFV_V_按pv汇总_201805.csv',3)#

dt.4.all<-fun1('RFV_FV全量清单_201806.csv',
               c('mobile','ck',"xia","zhong","shang","rfv_f","vv_sum"),
               c('mobile','changke',"xia","zhong","shang","days","vv_sum"),
               'RFV_V_按pv汇总_201806.csv',4)#

dt.4.all<-dt.3.all[dt.4.all,on='mobile']
dt.4.all[is.na(dt.4.all)]<-0
rm(dt.3.all)

dt.4.all<-dt.4.all[,avg_sum3:=vv_sum3/days3]
dt.4.all<-dt.4.all[,avg_sum4:=vv_sum4/days4]
dt.4.all[is.na(dt.4.all)]<-0

test<-dt.4.all[,.(
  mobile,
  busi=业务4-业务3,
  qiandao=签到4-签到3,
  chaxun=查询4-查询3,
  daohang=底部切换4-底部切换3,
  digital=数字化内容4-数字化内容3,
  changke=changke3+changke4,
  shang=shang4-shang3,
  zhong=zhong4-zhong3,
  xia=xia4-xia3,
  days=days4-days3,
  vv_sum=vv_sum4-vv_sum3,
  avg_sum=avg_sum4-avg_sum3
)]

dt.4.all<-test[dt.4.all,on='mobile']
dt.4.all[is.na(dt.4.all)]<-0
rm(test)

#选中参数，开始预测
cl.LR.SELECT<-c('chaxun','changke','签到4','xia4','days4','avg_sum4')
test.LR.TEMP<-as.data.frame(dt.4.all)
test.LR.TEMP<-test.LR.TEMP[,c("mobile",cl.LR.SELECT)]

c.pred<-predict(model_LR,test.LR.TEMP,type='response')
c.pred <- ifelse( c.pred > 0.5,1,0)


test.LR.TEMP$pred<-c.pred
test.LR.TEMP<-test.LR.TEMP[,c("mobile","pred")]
test.LR.TEMP$mobile<-as.character(test.LR.TEMP$mobile)
write.csv(test.LR.TEMP,"201806活跃用户在201807流失预测.csv",row.names = F)





